package com.miyu.client1.dao.book;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.miyu.client1.pojo.book.BookChapter;
import com.miyu.client1.pojo.book.BookListInfo;
import com.miyu.client1.utils.baseDao.BaseDao;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Repository
public interface bookMagicDao extends BaseDao<BookListInfo,String> {



    public default void saveBookListInfo(JSONArray bookListInfo){
        List<BookListInfo>info = bookListInfo.toJavaList(BookListInfo.class);
        JSONObject da = this.getData();
        if(!da.isEmpty()){
            //排除过滤数据库已存在的数据
            for (int i = 0; i < info.size(); i++) {
                BookListInfo  detail= info.get(i);
                if (da.containsKey(detail.getBookAuthor()+"@@"+""+detail.getBookName())){
                    info.remove(i);
                    i--;
                }
            }
            if(info.size()>0)
                saveBatch(info);
        }else{
            saveBatch(info);
        }

    }


    /**
     * 数据筛选转换
     * @param
     * @return
     */
    public default JSONObject getData() {
        List<BookListInfo> list = findAll();
        JSONObject jo = new JSONObject();
        for (BookListInfo bli: list) {
            //校验书名和作者相同 即为同一本书 不在做赛选
            jo.put(bli.getBookAuthor()+"@@"+bli.getBookName(),bli);
        }
        return jo;
    }

    default void saveBookChapter(JSONArray jo){
        List<BookChapter>info = jo.toJavaList(BookChapter.class);

    }

    public default JSONArray queryBookList(JSONObject param){

        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT bli.book_id, br.sys_source_name, book_name,book_author,book_kind,book_state,book_duce,book_source ");
        sql.append(" FROM book_list_info bli left join book_rule br on bli.book_rule_source = br.book_id");
        sql.append("  where 1=1 ");
        if (param.containsKey("search")){
            if(param.getString("search").length()>0)
                sql.append(" and (book_name like '%").append(param.getString("search")).append("%' or book_author like '").append(param.getString("search")).append("%'  )");
        }
        sql.append(" limit 0,300");
        List<Map<String, Object>> maps = this.queryListBySql(sql.toString());
        JSONArray rs = JSONArray.parseArray(JSON.toJSONString(maps));
        return rs;
    }

    /**
     * 判断当前网站是否完全
     * @param str
     * @return
     */
    public default JSONObject querybookSource(String str){
        JSONObject ds = new JSONObject();
        StringBuffer sql = new StringBuffer();
        sql.append(" select book_id,book_rule_source,book_name,cast (REPLACE (book_source,'http://147xs.org/book/' ,'') as int)  ts from book_list_info");
      //  sql.append(" select book_id,book_rule_source,book_name,CAST(substring(book_source, 23) as SIGNED)  ts from book_list_info  ");
        sql.append(" where book_rule_source='").append(str).append("'");
        sql.append(" order by ts");
        List<Map<String, Object>> mpList = this.queryListBySql(sql.toString());
        if(mpList.size()>0){
            for (Map<String,Object> map: mpList
            ) {
                ds.put(map.get("ts").toString(),map);
            }
        }
        return ds;
    }

    public  default void saveOrUpdateBatcn(List<String> sqlList) {
        this.doExecuteBatch(sqlList);
    }

    public default void  updateChapterById(String sql){
        this.doExecute(sql);
    }
}
